package com.vpen.blog.controller;


import com.vpen.blog.common.ReturnResult;
import com.vpen.blog.entity.Blog;
import com.vpen.blog.producer.DelayMessageProducer;
import com.vpen.blog.service.BlogService;
import com.vpen.blog.utils.BlogUtils;
import com.vpen.blog.utils.FileUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Vpen
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/api/article")
public class BlogController {

    @Autowired
    BlogService blogService;

    @Resource
    DelayMessageProducer  delayMessageProducer;

    @Resource
    BlogUtils blogUtils;

    /**
     * 作者:Vpen
     * 描述:添加文章
     * */
    @PostMapping("/add")
    public ReturnResult addArticle(@RequestBody Map<String,Object> article,
                                   HttpServletRequest request){
        Long userId = blogUtils.getUserId(request);// 获取token里面的id
        article.put("userId",userId);
        return doAdd(article);
    }
    /**
     * 作者:Vpen
     * 描述:显示文章
     * */
    @GetMapping("/list")
    public ReturnResult getArticles(){
        List<Blog> articles = blogService.getArticles();
        return ReturnResult.getInstance().success(articles);
    }
    /**
     * 作者:Vpen
     * 描述:删除文章
     * */
    @PostMapping("/delete")
    public ReturnResult deleteArticle(@RequestBody Map<String,Long> map){
        Long id = map.get("id");
        Boolean res = blogService.deleteArticle(id);
        if (res){
            return ReturnResult.getInstance().success("删除成功o((>ω< ))o",null);
        }
        return ReturnResult.getInstance().fail("删除失败w(ﾟДﾟ)w");
    }

    @PostMapping("/upload")
    public ReturnResult upLoadImg(@RequestParam("file") MultipartFile file,
                                  HttpServletRequest request){
        try {
            Long userId = blogUtils.getUserId(request);// 获取token里面的id
            HashMap<String, Object> article = FileUtils.parseArticleFile(file);
            article.put("userId",userId);
          return blogService.addArticle(article);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return ReturnResult.getInstance().success("上传成功",null);
    }

    /**
    * -@author Vpen
    * -@name doAdd
    * -@date 2021/7/12 14:31
    * -@param [article]
    * -@return com.vpen.blog.common.ReturnResult
    * -@作用:判断是否延迟提交，
    */
    private ReturnResult doAdd(Map<String,Object> article){
        try {
            String time = (String) article.get("delayTime");
            System.out.println("time = " + time);
            Integer delayTime= Integer.parseInt(time);
            System.out.println(delayTime);
            if (delayTime==0){// 没有延迟提交，当场添加到数据库
                return blogService.addArticle(article);
            }
            if (delayTime>0){// 延迟提交
                // 生产者发送文章给消费者，等待时间超时后，向数据库添加
                delayMessageProducer.send(article,delayTime);
            }
            return ReturnResult.getInstance().success("提交成功，文章将会在"+delayTime+"秒内发表",null);
        } catch (Exception e) {
            return ReturnResult.getInstance().fail("请输入正确的数字");
        }
    }

}
